<?php

namespace App\Admin\Actions\ShopOrderReturn;

use App\Library\Logic\OperateLogLogic;
use App\Library\Service\Payment\WechatService;
use App\Models\ShopOrderReturnModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Exception;

class ShopOrderReturnConfirmAction extends RowAction
{
    public $name = '确认';

    /**
     * handle
     * @param ShopOrderReturnModel $model
     * @return Response
     * @throws Exception
     */
    public function handle(ShopOrderReturnModel $model): Response
    {
        if ((int)$model->or_buy_status !== 4) $this->response()->error('参数错误')->refresh();
        $model->or_approved_time = time();
        $model->or_sell_status = 5;
        $model->or_buy_status = 5;
        $model->save();
        $model->order->order_sell_status = 5;
        $model->order->order_buy_status = 5;
        $model->order->save();
        payLog(WechatService::refund($model)->toArray());
        OperateLogLogic::add(Admin::user()->user_id, 'admin.orderReturn.confirm');
        ShopOrderReturnAuction::salesHandle($model, '已同意');
        return $this->response()->success('操作成功')->refresh();
    }

    public function dialog(): void
    {
        $this->confirm('是否确定?');
    }
}
